package com.eleadmin.order.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.OceanFeeBiz;
import com.eleadmin.order.entity.OceanFee;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2023-04-24 11:29:17
 * @version ：1.0
 */
@Repository
public class OceanFeeBizImpl extends BaseDao implements OceanFeeBiz {

    @Override
    public JsonResult list(PageResult2<T> page, OceanFee oceanFee, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<OceanFee> rm = new BeanPropertyRowMapper<OceanFee>(OceanFee.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
        sql.append(",o.type,o.fee_name,o.price,o.fee_modules,o.fee_modules_order");
		sql.append(",o.invoicename,o.invoicestate,o.currency,o.exchange_rate");
		sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_ocean_fee o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=o.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 and o.ocean_id=:ocean_id");
        map.put("ocean_id", oceanFee.getOceanId());
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(oceanFee)){
            if(Utils.isNotEmpty(oceanFee.getType())){
                sql.append(" and instr(o.type,:type)>0 ");
                map.put("type", oceanFee.getType());
            }
            if(Utils.isNotEmpty(oceanFee.getSort())&&Utils.isNotEmpty(oceanFee.getOrder())){
                sql.append(" order by o."+oceanFee.getSort()+" "+oceanFee.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<OceanFee> list(OceanFee oceanFee, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<OceanFee> rm = new BeanPropertyRowMapper<OceanFee>(OceanFee.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
        sql.append(",o.type,o.fee_name,o.price,o.fee_modules,o.fee_modules_order");
		sql.append(",o.invoicename,o.invoicestate,o.currency,o.exchange_rate");
		sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_fee o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 and o.ocean_id=:ocean_id");
        map.put("ocean_id", oceanFee.getOceanId());
        if(Utils.isNotEmpty(oceanFee)){
            if(Utils.isNotEmpty(oceanFee.getType())){
                sql.append(" and instr(o.type,:type)>0 ");
                map.put("type", oceanFee.getType());
            }
            if(Utils.isNotEmpty(oceanFee.getSort())&&Utils.isNotEmpty(oceanFee.getOrder())){
                sql.append(" order by o."+oceanFee.getSort()+" "+oceanFee.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public OceanFee getById(String id) {
        RowMapper<OceanFee> rm = new BeanPropertyRowMapper<OceanFee>(OceanFee.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
        sql.append(",o.type,o.fee_name,o.price,o.fee_modules,o.fee_modules_order");
		sql.append(",o.invoicename,o.invoicestate,o.currency,o.exchange_rate");
		sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
		sql.append(",o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_fee o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 and o.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    public String getOrder(String feeModulesOrder) {
        if(feeModulesOrder.equals("运费")) { return "100"; }
        if(feeModulesOrder.equals("提箱")) { return "200"; }
        if(feeModulesOrder.equals("进港")) { return "300"; }
        if(feeModulesOrder.equals("其他")) { return "400"; }
        if(feeModulesOrder.equals("单项")) { return "500"; }
        if(feeModulesOrder.equals("采购")) { return "600"; }
        if(feeModulesOrder.equals("报关费")) { return "700"; }
        if(feeModulesOrder.equals("易港通")) { return "800"; }
        if(feeModulesOrder.equals("财务报销")) { return "900"; }
        return "1";
    }

    @Override
    public boolean save(OceanFee oceanFee, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into chf_ocean_fee (");
        sql.append("organization_id,ocean_id,type,fee_name");
		sql.append(",price,fee_modules,fee_modules_order,invoicename,invoicestate");
		sql.append(",currency,exchange_rate,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:ocean_id,:type,:fee_name");
		sql.append(",:price,:fee_modules,:fee_modules_order,:invoicename,:invoicestate");
		sql.append(",:currency,:exchange_rate,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("ocean_id", oceanFee.getOceanId());
        map.put("type", oceanFee.getType());
        map.put("fee_name", oceanFee.getFeeName());
        map.put("price", oceanFee.getPrice());
        map.put("fee_modules", oceanFee.getFeeModules());
        map.put("fee_modules_order", getOrder(oceanFee.getFeeModules()));
        map.put("invoicename", oceanFee.getInvoicename());
        map.put("invoicestate", oceanFee.getInvoicestate());
        map.put("currency", oceanFee.getCurrency());
        map.put("exchange_rate", oceanFee.getExchangeRate());
        map.put("remark", oceanFee.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(OceanFee oceanFee, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update chf_ocean_fee set ");
        sql.append("ocean_id=:ocean_id,type=:type,fee_name=:fee_name ");
		sql.append(",price=:price,fee_modules=:fee_modules,fee_modules_order=:fee_modules_order,invoicename=:invoicename ");
		sql.append(",invoicestate=:invoicestate,currency=:currency,exchange_rate=:exchange_rate ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("ocean_id", oceanFee.getOceanId());
        map.put("type", oceanFee.getType());
        map.put("fee_name", oceanFee.getFeeName());
        map.put("price", oceanFee.getPrice());
        map.put("fee_modules", oceanFee.getFeeModules());
        map.put("fee_modules_order", getOrder(oceanFee.getFeeModules()));
        map.put("invoicename", oceanFee.getInvoicename());
        map.put("invoicestate", oceanFee.getInvoicestate());
        map.put("currency", oceanFee.getCurrency());
        map.put("exchange_rate", oceanFee.getExchangeRate());
        map.put("remark", oceanFee.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", oceanFee.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(id.length()>0){
            for(String id1 : id.split(",")){
                map  = new HashMap<String, String>();
                if(id1==null || id1.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update chf_ocean_fee set deleted=1" +
                    ",update_user=:update_user, update_time=now() "
                    + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}
